#include <dspSimulator/command/printBReg.h>
#include <dspSimulator/dspSimulator/dspSimulator.h>


void PrintBRegCommand::execute(std::string command) {
    auto dspSimulator = dynamic_cast<DSPSimulator *>(m_simulator);
    if (dspSimulator == nullptr) {
        fatalMacro("该模拟器对象不为dsp模拟器对象!");
    }
    auto targetMachine = dspSimulator->m_targetMachine;
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (dspMachine == nullptr) {
        fatalMacro("该目标机对象不为dsp目标机对象!");
    }

    // 如果输入以regB开头，打印对应的寄存器B的值
    if (command.size() < 5) {
        requestReEnter;
        return;
    }
    u32 num = stoul(command.substr(4));
    if (num >= static_cast<u32>(RegBKind::NUM_OF_Reg_B)) {
        fprintf(stdout, "不存在的寄存器B%d\n", num);
    }
    fprintf(stdout, "%s : %08X\n", RegBName[num],
            *dspMachine->getRegisters()->getReg32ByName(RegBName[num])->getDataPtr());
}

std::shared_ptr<PrintBRegCommand> PrintBRegCommand::get(Simulator* dspSimulator) {
    static std::shared_ptr<PrintBRegCommand> printBRegCommand;
    if (printBRegCommand == nullptr) {
        printBRegCommand = std::make_shared<PrintBRegCommand>(dspSimulator);
    }
    return printBRegCommand;
}
